//
//  ServiceTableSectionHeadView.m
//  268EDU_Demo
//
//  Created by 刘星 on 2017/7/31.
//  Copyright © 2017年 edu268. All rights reserved.
//

#import "ServiceTableSectionHeadView.h"
#import "Masonry.h"

@interface ServiceTableSectionHeadView ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *lineView1;
@property (nonatomic, strong) UIView *lineView2;
@property (nonatomic, strong) UIImageView *backgroudImageView;

@end

@implementation ServiceTableSectionHeadView

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    [self addSubview:self.backgroudImageView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.lineView1];
    [self addSubview:self.lineView2];
    
    self.titleLabel.text = @"专业必修课";
    
    [self.backgroudImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self);
    }];
    
    [self.lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self.titleLabel.mas_left).offset(-15);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(1);
    }];
    
    [self.lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self.titleLabel.mas_right).offset(15);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(1);
    }];
}

- (void)setTitle:(NSString *)title {
    _title = [title copy];
    self.titleLabel.text =_title;
    NSArray *array = @[@"service_ required_course-backgroud", @"service_ optional_course-backgroud", @"service_master_backgroud", @"service_live_backgroud"];
    int x = arc4random()% 4;
    self.backgroudImageView.image = [UIImage imageNamed:array[x]];
    if ([_title isEqualToString:@"本周推荐"]) {
        self.backgroudImageView.image = [UIImage imageNamed:@"home_hot_background"];
    }
}

- (UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont boldSystemFontOfSize:17];
        _titleLabel.textColor = UIColorFromRGB(0xffffff);
    }
    return _titleLabel;
}

- (UIView *)lineView1 {
    if (_lineView1 == nil) {
        _lineView1 = [[UIView alloc] init];
        _lineView1.backgroundColor = UIColorFromRGB(0xffffff);
    }
    return _lineView1;
}

- (UIView *)lineView2 {
    if (_lineView2 == nil) {
        _lineView2 = [[UIView alloc] init];
        _lineView2.backgroundColor = UIColorFromRGB(0xffffff);
    }
    return _lineView2;
}

- (UIImageView *)backgroudImageView {
    if (_backgroudImageView == nil) {
        _backgroudImageView = [[UIImageView alloc] init];
    }
    return _backgroudImageView;
}

@end
